package com.example.aiscripteditorserver.common.tools.excel;

import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ExcelReader {

    /**
     * 读取 Excel 第 1、2 列（跳过首行）→ Map<物品名称, 监控最低价格>
     */
    public static Map<String, Double> readCol1Col2(File excel) throws Exception {
        Map<String, Double> map = new HashMap<>();
        Workbook wb = WorkbookFactory.create(excel);
        Sheet sheet = wb.getSheetAt(0);          // 第一张表
        for (Row row : sheet) {
            if (row.getRowNum() == 0) continue;  // 跳过表头
            Cell c0 = row.getCell(0);
            Cell c1 = row.getCell(1);
            if (c0 == null || c1 == null) continue;

            String name  = c0.getStringCellValue().trim();
            double price = c1.getNumericCellValue(); // 如果是字符串列用 .toString()
            map.put(name, price);
        }
        wb.close();
        return map;
    }

    /* ---------------- 测试 ---------------- */
    public static void main(String[] args) throws Exception {
        Map<String, Double> priceMap = readCol1Col2(new File("价格表.xlsx"));
        priceMap.forEach((k, v) -> System.out.println(k + " -> " + v));
    }
}